<template>
  <div class="user-info">
    <!-- 个人信息 -->
    <el-form ref="userForm" label-width="220px" :model="userInfo">
      <!--手机 -->

      <el-form-item label="手机">
        <el-input v-model="userInfo.mobile" style="width:220px" />
      </el-form-item>

      <!-- 工号 入职时间 -->

      <el-form-item label="入职时间">
        <el-date-picker
          v-model="userInfo.timeOfEntry"
          type="date"
          class="inputW"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>

      <!-- 员工照片 -->

      <el-form-item label="员工头像">
        <!-- 放置上传图片 -->
        <UploadImg v-model="userInfo.staffPhoto" />
      </el-form-item>

      <!-- 保存个人信息 -->
      <el-form-item>
        <el-button type="primary" @click="hSave">保存更新</el-button>
        <el-button @click="$router.back()">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { getEmployeesDetail, updateEmployee } from '@/api/employees'
export default {
  data() {
    return {
      userInfo: {
        id: this.$route.query.id,
        mobile: '',
        timeOfEntry: '',
        staffPhoto: ''
      }

    }
  },
  created() {
    this.loadUserDatailById()
  },
  methods: {
    async loadUserDatailById() {
      //
      const id = this.$route.query.id// 从地址栏拿到id
      console.log(id)
      const res = await getEmployeesDetail(this.userInfo.id)
      console.log('res.data', res.data)
      this.userInfo = res.data
    //   this.userInfo.mobile = res.data.mobile
    //   this.userInfo.timeOfEntry = res.data.timeOfEntry
    //   this.userInfo.staffPhoto = res.data.staffPhoto
    },
    async doSave() {
      try {
        await updateEmployee(this.userInfo)
        this.$message.success('修改成功')
      } catch (err) {
        this.$message.error(err.message)
      }
    },
    hSave() {
      // 表单校验
      this.$refs.userForm.validate(valid => {
        valid && this.doSave()
      })
    }
  }
}
</script>
